﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input.Touch;

namespace WindowsGame.Input
{
	/// <summary>
	/// PhoneInputFactory is responsible for managing Windows Phone input.
	/// </summary>
	public class PhoneInputFactory : AInputFactory
	{
		public override Single Rotate()
		{
			Single movement = 0;

			Vector2 touchPosition = GetTouchPosition();
			if (touchPosition != Vector2.Zero)
			{
				Single x = touchPosition.X;
				Single y = touchPosition.Y;

				if (y >= 240)
				{
					if (x < 400)
					{
						movement = -1;
					}
					else if (x >= 400)
					{
						movement = 1;
					}
				}
			}

			return movement;
		}

		public override Single Move()
		{
			Single acceleration = 0;

			Vector2 touchPosition = GetTouchPosition();
			if (touchPosition != Vector2.Zero)
			{
				Single x = touchPosition.X;
				Single y = touchPosition.Y;

				if (y < 240)
				{
					if (x >= 400)
					{
						acceleration = 1;
					}
				}
			}

			return acceleration;
		}

		public override Boolean Warp()
		{
			Boolean warp = false;

			Vector2 touchPosition = GetTouchPosition();
			if (touchPosition != Vector2.Zero)
			{
				Single x = touchPosition.X;
				Single y = touchPosition.Y;

				if (y < 240)
				{
					if (x < 400)
					{
						warp = true;
					}
				}
			}

			return warp;
		}

		private static Vector2 GetTouchPosition()
		{
			Vector2 touchPosition = Vector2.Zero;

			TouchCollection touchCollection = TouchPanel.GetState();
			if (touchCollection.Count > 0)
			{
				TouchLocation location = touchCollection[0];
				touchPosition = location.Position;
			}

			return touchPosition;
		}
	}
}